package com.zj.leetcode;

public class A459重复的字符子串 {

    public static void main(String[] args) {
        String ss = "22";

        System.out.println(repeatedSubstringPattern2(ss));
    }

    public static boolean repeatedSubstringPattern(String s) {
        if (s != null && s.length() > 1) {
            for (int i = 1; i < s.length() / 2 + 1; i++) {
                if (s.charAt(i) == s.charAt(0) && trySubstringPattern(s, i)) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean trySubstringPattern(String s, int end) {
        if (s.length() % end > 0) {
            return false;
        }
        int repeat = s.length() / end;
        for (int j = 0; j < end; j++) {
            for (int i = 1; i < repeat; i++) {
                if (s.charAt(j) != s.charAt(j + i * end)) return false;
            }
        }
        return true;

    }



/*解法2，从长度找到能整数的数*/
    public static boolean repeatedSubstringPattern2(String s) {
        if (s != null && s.length() > 1) {
            for (int i = 2; i < s.length() +1; i++) {
                if (s.length()%i==0&&trySubstringPattern(s, s.length()/i)) {
                    return true;
                }
            }
        }
        return false;
    }
}
